package com.xilihui.xlh.core.glide;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.app.Fragment;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.xilihui.xlh.R;
import com.xilihui.xlh.core.util.DensityUtil;
import com.xilihui.xlh.core.util.SPUtil;

public class ImageHelper {

    /**
     * 是否wifi连接
     *
     * @param context
     * @return
     */
    private static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
                return networkInfo.isAvailable();
        }
        return false;
    }

    /**
     * 非wifi环境下是否加载图片
     *
     * @param context
     * @return
     */
    private static boolean isEnabled(Context context) {
        return (boolean) SPUtil.get(context, SPUtil.isLoadImageIn4G, true);
    }

    public static void display(final Context context, final ImageView imageView, final String url) {
        if (isWifiConnected(context) || isEnabled(context))
            Glide.with(context)
                    .load(url)
                    .crossFade(500)
                    .placeholder(R.mipmap.ic_default_square)
                    .error(R.mipmap.ic_default_square)
                    .into(imageView);
    }

    public static void display(final Activity activity, final ImageView imageView, final String url) {
        if (isWifiConnected(activity) || isEnabled(activity))
            Glide.with(activity)
                    .load(url)
                    .crossFade(500)
                    .placeholder(R.mipmap.ic_default_square)
                    .error(R.mipmap.ic_default_square)
                    .into(imageView);
    }

    public static void display(final Fragment fragment, final ImageView imageView, final String url) {
        if (isWifiConnected(fragment.getContext()) || isEnabled(fragment.getContext()))
            Glide.with(fragment)
                    .load(url)
                    .crossFade(500)
                    .placeholder(R.mipmap.ic_default_square)
                    .error(R.mipmap.ic_default_square)
                    .into(imageView);
    }

    public static void displayCircle(final Context context, final ImageView imageView, final String url) {
        if (isWifiConnected(context) || isEnabled(context))
            Glide.with(context)
                    .load(url)
                    .placeholder(R.mipmap.ic_default_square)
                    .error(R.mipmap.ic_default_square)
                    .dontAnimate()// 圆形头像
                    .into(imageView);
    }

    public static void displayCircle(final Fragment fragment, final ImageView imageView, final String url) {
        if (isWifiConnected(fragment.getContext()) || isEnabled(fragment.getContext()))
            Glide.with(fragment)
                    .load(url)
                    .placeholder(R.mipmap.ic_default_square)
                    .error(R.mipmap.ic_default_square)
                    .dontAnimate()// 圆形头像
                    .into(imageView);
    }

    public static void displayBanner(final Activity activity, final ImageView imageView, final String url) {
        if (isWifiConnected(activity) || isEnabled(activity))
            Glide.with(activity)
                    .load(url)
                    .crossFade(500)
                    .placeholder(R.mipmap.ic_default_rec)
                    .error(R.mipmap.ic_default_rec)
                    .into(imageView);
    }

    public static void displayBanner(final Context activity, final ImageView imageView, final String url) {
        if (isWifiConnected(activity) || isEnabled(activity))
            Glide.with(activity)
                    .load(url)
                    .crossFade(500)
                    .placeholder(R.mipmap.ic_default_rec)
                    .error(R.mipmap.ic_default_rec)
                    .into(imageView);
    }

    public static void displayBanner(final Fragment fragment, final ImageView imageView, final String url) {
        if (isWifiConnected(fragment.getContext()) || isEnabled(fragment.getContext()))
            Glide.with(fragment)
                    .load(url)
                    .crossFade(500)
                    .placeholder(R.mipmap.ic_default_rec)
                    .error(R.mipmap.ic_default_rec)
                    .into(imageView); 
    }


    public static void displayAvatars(final Context context, final ImageView imageView, final String url) {
        if (isWifiConnected(context) || isEnabled(context))
            Glide.with(context)
                    .load(url)
                    .placeholder(R.mipmap.ic_avatars_default)
                    .error(R.mipmap.ic_avatars_default)
                    .dontAnimate()// 圆形头像
                    .into(imageView);
    }

    public static void displayAvatars(final Activity activity, final ImageView imageView, final String url) {
        if (isWifiConnected(activity) || isEnabled(activity))
            Glide.with(activity)
                    .load(url)
                    .placeholder(R.mipmap.ic_avatars_default)
                    .error(R.mipmap.ic_avatars_default)
                    .dontAnimate()// 圆形头像
                    .into(imageView);
    }

    public static void displayAvatars(final Fragment fragment, final ImageView imageView, final String url) {
        if (isWifiConnected(fragment.getContext()) || isEnabled(fragment.getContext()))
            Glide.with(fragment)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.mipmap.ic_avatars_default)
                    .error(R.mipmap.ic_avatars_default)
                    .dontAnimate()// 圆形头像
                    .into(imageView);
    }

    /**
     * 显示TextView的图片
     *
     * @param context
     * @param tv
     * @param url
     */
    public static void display(final Context context, final TextView tv, String url) {
        if (isWifiConnected(context) || isEnabled(context))
            Glide.with(context)
                    .load(url)
                    .asBitmap()
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            BitmapDrawable drawable = new BitmapDrawable(context.getResources(), resource);
                            drawable.setBounds(0, 0, DensityUtil.dp2px(context, 70), DensityUtil.dp2px(context, 70));
                            tv.setCompoundDrawables(null, drawable, null, null);
                        }
                    });
    }

    /**
     * 显示本地图片
     *
     * @param activity
     * @param imageView
     * @param path
     */
    public static void displayLocal(Activity activity, ImageView imageView, String path) {
        Glide.with(activity)
                .load(path)
                .centerCrop()
                .crossFade()
                .into(imageView);
    }

    /**
     * 显示圆角图片
     *
     * @param context
     * @param iv
     * @param url
     */
    public static void displayRounded(final Context context, final ImageView iv, String url, int dp) {
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.ic_default_square)
                .error(R.mipmap.ic_default_square)
                .transform(new CenterCrop(context), new GlideRoundTransform(context, dp))
                .into(iv);
    }

    /**
     * 显示圆角图片，默认圆角 3dp
     *
     * @param context
     * @param iv
     * @param url
     */
    public static void displayRounded(final Context context, final ImageView iv, String url) {
        ImageHelper.displayRounded(context, iv, url, DensityUtil.dp2px(context, 3));
    }

    /**
     * 显示圆角图片
     *
     * @param activity
     * @param iv
     * @param url
     * @param dp
     */
    public static void displayRounded(final Activity activity, final ImageView iv, String url, int dp) {
        Glide.with(activity)
                .load(url)
                .placeholder(R.mipmap.ic_default_square)
                .error(R.mipmap.ic_default_square)
                .transform(new CenterCrop(activity), new GlideRoundTransform(activity, dp))
                .into(iv);
    }

    /**
     * 显示圆角图片，默认圆角 3dp
     *
     * @param activity
     * @param iv
     * @param url
     */
    public static void displayRounded(final Activity activity, final ImageView iv, String url) {
        ImageHelper.displayRounded(activity, iv, url, DensityUtil.dp2px(activity, 3));
    }

    /**
     * 显示圆角图片
     *
     * @param fragment
     * @param iv
     * @param url
     * @param dp
     */
    public static void displayRounded(final Fragment fragment, final ImageView iv, String url, int dp) {
        Glide.with(fragment)
                .load(url)
                .placeholder(R.mipmap.ic_default_square)
                .error(R.mipmap.ic_default_square)
                .transform(new CenterCrop(fragment.getContext()), new GlideRoundTransform(fragment.getContext(), dp))
                .into(iv);
    }

    /**
     * 显示圆角图片，默认 3dp
     *
     * @param fragment
     * @param iv
     * @param url
     */
    public static void displayRounded(final Fragment fragment, final ImageView iv, String url) {
        ImageHelper.displayRounded(fragment, iv, url, DensityUtil.dp2px(fragment.getContext(), 3));
    }
}
